package com.example.basicknowledge.弹出对话框;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.example.basicknowledge.BasicKnowledge;
import com.example.basicknowledge.R;

public class UsageOfAlertDialog extends AppCompatActivity implements AdapterView.OnItemClickListener{
    private String[] data = { "普通对话框","列表对话框","单选列表","多选列表","自定对话框","完全自定义对话框","三个按钮对话框","预定样式对话框（通过修改AndroidManifest.xml文件中对应activity配置实现）","自定义样式对话框，自定义style"};
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.usage_of_alert_dialog);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                UsageOfAlertDialog.this, android.R.layout.simple_list_item_1, data);
        ListView listView = (ListView) findViewById(R.id.alertList);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        switch (adapterView.getId()) {
            case R.id.alertList:
                System.out.println(i);
                break;
        }
        if (adapterView.getId() == R.id.alertList) {
            switch (i) {
                case 0:
                    final AlertDialog dialog = new AlertDialog.Builder(this)
                            .setIcon(R.mipmap.ic_launcher)
                            .setTitle("我是普通对话框").setMessage("我是对话框的内容")
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Toast.makeText(UsageOfAlertDialog.this,"点击了取消按钮",Toast.LENGTH_SHORT).show();
                            dialogInterface.dismiss();
                        }
                    }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Toast.makeText(UsageOfAlertDialog.this,"点击确定按钮",Toast.LENGTH_SHORT).show();
                            dialogInterface.dismiss();
                        }
                    }).create();
                    dialog.show();
                    break;
                case 1:
                    final String[] items = {"我是Item一","我是Item二","我是Item三","我是Item四"};
                    AlertDialog dialog1 = new AlertDialog.Builder(this)
                            .setIcon(R.mipmap.ic_launcher)
                            .setTitle("我是列表对话框")
                            .setItems(items, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Toast.makeText(UsageOfAlertDialog.this,items[i],Toast.LENGTH_SHORT).show();
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Toast.makeText(UsageOfAlertDialog.this,"点击了取消按钮",Toast.LENGTH_SHORT).show();
                                    dialogInterface.dismiss();
                                }
                            }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Toast.makeText(UsageOfAlertDialog.this,"点击确定按钮",Toast.LENGTH_SHORT).show();
                                    dialogInterface.dismiss();
                                }
                            }).create();
                    dialog1.show();
                    break;
                case 2:
                    final String[] items1 = {"我是Item一","我是Item二","我是Item三","我是Item四"};
                    AlertDialog dialog2 = new AlertDialog.Builder(this)
                            .setIcon(R.mipmap.ic_launcher)
                            .setTitle("我是单选列表对话框")
                            .setSingleChoiceItems(items1,1,new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Toast.makeText(UsageOfAlertDialog.this,items1[i],Toast.LENGTH_SHORT).show();
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Toast.makeText(UsageOfAlertDialog.this,"点击了取消按钮",Toast.LENGTH_SHORT).show();
                                    dialogInterface.dismiss();
                                }
                            }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Toast.makeText(UsageOfAlertDialog.this,"点击确定按钮",Toast.LENGTH_SHORT).show();
                                    dialogInterface.dismiss();
                                }
                            }).create();
                    dialog2.show();
                    break;
                case 3:
                    final String[] items2 = {"我是Item一","我是Item二","我是Item三","我是Item四"};
                    final boolean checkedItems[] = {true, false, true, false};
                    AlertDialog dialog3 = new AlertDialog.Builder(this)
                            .setIcon(R.mipmap.ic_launcher)
                            .setTitle("我是多选列表对话框")
                            .setMultiChoiceItems(items2, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                                    checkedItems[i] = b;
                                    if (b) {
                                        Toast.makeText(UsageOfAlertDialog.this,"选中" + items2[i],Toast.LENGTH_SHORT)
                                                .show();
                                    }
                                    else {
                                        Toast.makeText(UsageOfAlertDialog.this,"取消" + items2[i],Toast.LENGTH_SHORT)
                                                .show();
                                    }
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Toast.makeText(UsageOfAlertDialog.this,"点击了取消按钮",Toast.LENGTH_SHORT).show();
                                    dialogInterface.dismiss();
                                }
                            }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Toast.makeText(UsageOfAlertDialog.this,"点击确定按钮",Toast.LENGTH_SHORT).show();
                                    dialogInterface.dismiss();
                                }
                            }).create();
                    dialog3.show();
                    break;
                case 4:
                    View view1 = getLayoutInflater().inflate(R.layout.custom_alert_dialog_input_view,null);
                    final EditText userText = (EditText)view1.findViewById(R.id.et_user);
                    final EditText password = (EditText)view1.findViewById(R.id.et_password);
                    final AlertDialog dialog4 = new AlertDialog.Builder(this)
                            .setIcon(R.mipmap.ic_launcher)
                            .setTitle("我是自定义对话框").setView(view1)
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Toast.makeText(UsageOfAlertDialog.this,"点击了取消按钮",Toast.LENGTH_SHORT).show();
                                    dialogInterface.dismiss();
                                }
                            }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Toast.makeText(UsageOfAlertDialog.this,"点击确定按钮" + "账号：" + userText.getText().toString() + "密码：" + password.getText().toString(),Toast.LENGTH_SHORT).show();
                                    dialogInterface.dismiss();
                                }
                            }).create();
                    dialog4.show();
                    break;
                case 5:
                    final Dialog dialog5 = new Dialog(this,R.style.NormalDialogStyle);
                    View view2 = View.inflate(this,R.layout.custom_alert_dialog_input_view,null);
                    view2.setBackgroundColor(Color.WHITE);
                    final EditText userText1 = (EditText)view2.findViewById(R.id.et_user);
                    final EditText password1 = (EditText)view2.findViewById(R.id.et_password);
                    dialog5.setContentView(view2);
                    dialog5.setCanceledOnTouchOutside(true);
                    view2.setMinimumHeight((int)(getResources().getDisplayMetrics().heightPixels * 0.23f));

                    Window dialogWindow = dialog5.getWindow();
                    WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                    lp.width = (int)(getResources().getDisplayMetrics().widthPixels * 0.75f);
                    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                    //对话框出现方式控制
                    lp.gravity = Gravity.CENTER;
                    dialogWindow.setAttributes(lp);
                    dialog5.show();

                    break;

                case 6:
                    final AlertDialog dialog6 = new AlertDialog.Builder(this)
                            .setIcon(R.mipmap.ic_launcher)
                            .setTitle("我是三个按钮对话框").setMessage("我是对话框的内容")
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Toast.makeText(UsageOfAlertDialog.this,"点击了取消按钮",Toast.LENGTH_SHORT).show();
                                    dialogInterface.dismiss();
                                }
                            })
                            .setNeutralButton("忽略", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Toast.makeText(UsageOfAlertDialog.this,"点击忽略按钮",Toast.LENGTH_SHORT).show();
                                    dialogInterface.dismiss();
                                }
                            })
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Toast.makeText(UsageOfAlertDialog.this,"点击确定按钮",Toast.LENGTH_SHORT).show();
                                    dialogInterface.dismiss();
                                }
                            }).create();
                    dialog6.show();
                    break;
                case 7:
                    //AndroidManifest.xml中对activity设置android:theme="@android:style/Theme.Dialog"
                    Intent intent = new Intent(this,DialogActivity.class);
                    startActivity(intent);
                    break;
                case 8:
                    //AndroidManifest.xml中对activity设置android:theme="@android:style/Theme.Dialog"
                    Intent intent1 = new Intent(this,CustomActivityDialog.class);
                    startActivity(intent1);
                    break;
            }
        }
    }
}
